import api from "@/api/apiServer";
import { filterDate } from "@/unit/tool";

let sourceData;
async function getData(parentCode) {
  let res = await api.DictFindByCode({ parentCode });
  sourceData = res.data;
}
getData("dict.source");

// 格式化数据来源
function caseSource(value, option) {
  let result = option.filter((item) => {
    return item.id === value;
  });

  if (result.length > 0) {
    return result[0].dictName;
  } else {
    return "";
  }
}

let processingOption = [];
let pickingOption = [];
async function getProcessingOption(parentCode) {
  let res = await api.DictFindByCode({ parentCode });
  processingOption = res.data;
}
async function getPickingOption(parentCode) {
  let res = await api.DictFindByCode({ parentCode });
  pickingOption = res.data;
}
getProcessingOption("dict.processing", processingOption);
getPickingOption("dict.picking", pickingOption);

const planColumns = [
  {
    title: "序号",
    dataIndex: "index",
    key: "index",
    width: 100,
    align: "center",
    customRender: (text, record, index) => `${index + 1}`,
  },
  {
    title: "计划单号",
    dataIndex: "planNum",
    key: "planNum",
    align: "left",
    scopedSlots: { customRender: "productPlan" },
  },
  {
    title: "创建人",
    dataIndex: "createdBy",
    key: "createdBy",
    align: "center",

  },
  {
    title: "创建时间",
    dataIndex: "createtime",
    key: "createtime",
    align: "center",
  },
  // {
  //   title: "操作",
  //   width: 200,
  //   dataIndex: "action",
  //   scopedSlots: { customRender: "action" },
  // },
];

const procedureColumns = [
  {
    title: "工序序号",
    dataIndex: "workStageId",
    key: "workStageId",
    align: "center",
  },
  {
    title: "工序名称",
    dataIndex: "workStageName",
    key: "workStageName",
    align: "center",
  },
  {
    title: "机台",
    dataIndex: "equipNums",
    key: "equipNums",
    align: "center",
  },
  {
    title: "单价",
    dataIndex: "referencePrice",
    key: "referencePrice",
    align: "center",
  },
];

function caseStatus(status) {
  switch (status) {
    case 0:
      return "未完成";
    case 1:
      return "已完成";
  }
}

const goodColumns = [
  {
    title: "序号",
    dataIndex: "index",
    key: "index",
    customRender: (text, record, index) => `${index + 1}`,
    align: "center",
  },
  {
    title: "商品编号",
    dataIndex: "goodsCode",
    key: "goodsCode",
    align: "center",
  },
  {
    title: "商品名称",
    dataIndex: "goodsName",
    key: "goodsName",
    align: "center",
  },
  {
    title: "商品类型",
    key: "goodsTypeName",
    dataIndex: "goodsTypeName",
    align: "center",
  },
  {
    title: "计量单位",
    key: "unitName",
    dataIndex: "unitName",
    align: "center",
  },
  {
    title: "来源",
    key: "source",
    dataIndex: "source",
    align: "center",
    customRender: (text, record, index) => {
      return caseSource(text, sourceData);
    },
  },
  {
    title: "规格",
    key: "specifications",
    dataIndex: "specifications",
    align: "center",
  },
  {
    title: "品牌",
    key: "brand",
    dataIndex: "brand",
    align: "center",
  },
  {
    title: "是否生成BOM",
    key: "isUsedBom",
    dataIndex: "isUsedBom",
    customRender: (text, record, index) => {
      return caseBom(text);
    },
    align: "center",
  },
];

function caseBom(type) {
  switch (type) {
    case 0:
      return "否";
    case 1:
      return "是";
  }
}

const WorkOrderColumns = [
  {
    title: "序号",
    dataIndex: "index",
    key: "index",
    customRender: (text, record, index) => `${index + 1}`,
    align: "center",
  },
  {
    title: "加工单号",
    dataIndex: "processNum",
    key: "processNum",
    align: "center",
  },
  {
    title: "加工单数量",
    dataIndex: "productionQuantity",
    key: "productionQuantity",
    align: "center",
  },
  // {
  //   title: "完工数量",
  //   dataIndex: "rescri",
  //   key: "rescri",
  //   align: "center",
  // },
  // {
  //   title: "未完工数",
  //   dataIndex: "resc",
  //   key: "resc",
  //   align: "center",
  // },
  {
    title: "备注",
    dataIndex: "remark",
    key: "remark",
    align: "center",
  },
  {
    title: "入库数量",
    dataIndex: "amount",
    key: "remarks",
    align: "center",
  },
  {
    title: "备料",
    dataIndex: "pickStatus",
    key: "pickStatus",
    align: "center",
    customRender: (text, record, index) => {
      return caseSource(text, pickingOption);
    },
  },
  {
    title: "状态",
    dataIndex: "status",
    key: "status",
    align: "center",
    customRender: (text, record, index) => {
      return caseSource(text, processingOption);
    },
  },
  {
    title: "开始时间",
    dataIndex: "processStartTime",
    key: "processStartTime",
    align: "center",
    customRender: (text, record, index) => {
      return filterDate(text);
    },
  },
  {
    title: "结束时间",
    dataIndex: "processEndTime",
    key: "processEndTime",
    align: "center",
    customRender: (text, record, index) => {
      return filterDate(text);
    },
  },
];

export { planColumns, procedureColumns, goodColumns, WorkOrderColumns };
